#include "gw2maps.h"
#include <QtWidgets/QApplication>
#include "Console.h"
#include <string.h>
#include <Windows.h>
#include <iostream>
#include <tchar.h>

Console *pC;


int printHelp(char *app){
	std::cout << "start "<< app 
		<< " with:\n"
		"-i\t\tIP-Address\n-s\t\tServer-Name (URL)\n-p\t\tport only req if -i is set\n"
		"-t\t\tupdateInterval\n-u\t\tusername\n-c\t\tchannel\n-tracking\tdistance\n-g\t\tguild name\n"
		"-start\t\tuses predefined data specified in the gui\n"
		"-r\t\tapplies changes for the save-files\n"
		"-h/-?/-help\tshows the info\n" << std::endl << std::endl 
		<< "-i and -p are now supported, the transmission will not contain any HTTP-Headers" << std::endl;
	return 1;
}


void cleanup(int signal){
	pC->interrupt(signal);
}


int consoleInterface(int argc, char **argv){
	Console c;
	(void)signal(SIGINT, cleanup);
	pC = &c;
	int i, spec;
	spec = 0;
	for(i = 1; i < argc; i++){
		if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-?") == 0)
			return printHelp(argv[0]);
		else if(strcmp(argv[i], "-r") == 0)
			return c.reset();
		else if(strcmp(argv[i], "-l") == 0)
			return c.update();
		else if(strcmp(argv[i], "-v") == 0)
			return c.getVersion();
		else if(strcmp(argv[i], "-start") == 0)
			spec |= 0x80;
		else if(strcmp(argv[i], "-g") == 0){
			i++;
			c.setGuild(argv[i]);
		}
		else if(strcmp(argv[i], "-i") == 0){
			i++;
			c.setIp(argv[i]);
			spec |= 0x1;
		}
		else if(strcmp(argv[i], "-s") == 0){
			i++;
			c.setServer(argv[i]);
			spec |= 0x2;
		}
		else if(strcmp(argv[i], "-p") == 0){
			i++;
			c.setPort(argv[i]);
			spec |= 0x4;
		}
		else if(strcmp(argv[i], "-t") == 0){
			i++;
			c.setInterval(argv[i]);
		}
		else if(strcmp(argv[i], "-tracking") == 0){
			i++;
			c.setTracking(argv[i]);
		}
		else if(strcmp(argv[i], "-u") == 0){
			i++;
			c.setUser(argv[i]);
			spec |= 0x8;
		}
		else if(strcmp(argv[i], "-c") == 0){
			i++;
			c.setChannel(argv[i]);
		}
		else return printHelp(argv[0]);
	}
	
	switch(spec){
	case 0x2: printf("please specify your AccountId with -u <username>\n"); break;
	case 0x8: printf("please specify a server with -s <url>\n"); break;
	case 0xb: goto console;
	case 0xc: goto console;
	case 0xa: goto console;
	case 0x80: goto loadAndRun;
	case 0x3a: goto console;
	case 0x4a: goto console;
	case 0x7a: goto console;
	default: spec = printHelp(argv[0]); break;
	}
	return spec;
console:
	return c.exec();
loadAndRun:
	printf("loading data\n");
	if(c.load() != 0){
		printf("unable to load data\n");
		return 1;
	}
	printf("loading complete\n");
	return c.exec();
}


int checkDir(){
	TCHAR path[MAX_PATH];
	DWORD size = MAX_PATH;
	memset(path, 0, size);
	GetEnvironmentVariable(TEXT("APPDATA"), path, size);
	wcscat_s(path, size, TEXT(STD_DIR));
	return CreateDirectory(path, NULL);
}


void overrideUpdater(){
	std::ofstream out;
	char buffer;
	std::ifstream in(UPDATERV11, std::ios::in | std::ios::binary);
	if(in.fail())
		return;
	out.open(UPDATER_EXE, std::ios::out | std::ios::binary);
	while(!in.eof()){
		in.read(&buffer, 1);
		out.write(&buffer, 1);
	}
	in.close();
	out.close();
	remove(UPDATERV11);
}


int main(int argc, char **argv)
{
	overrideUpdater();
	checkDir();
	pC = 0;
	if(argc > 1){
		return consoleInterface(argc, argv);
	}
	else{
		ShowWindow(GetConsoleWindow(), SW_HIDE);
		QApplication a(argc, argv);
		Gw2Maps w;
		w.show();
		int result = a.exec();
		ShowWindow(GetConsoleWindow(), SW_SHOW);
		if(w.runUpdate())
			ShellExecute(NULL, NULL, TEXT(UPDATER_EXE), NULL, NULL, SW_NORMAL);
		return result;
	}
}
